<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/2/26
 * Time: 21:00
 */
//用户列表功能

require_once 'db.php';
require_once  'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 1;

if(!is_numeric($page) || !is_numeric($pageSize)) {
    return Response::show(401,'数据不合法');
}

//连接数据库
try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
//    $e->getMessage();
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

//分页的公式
$offset = ($page-1)*$pageSize;
$sql = "SELECT username,user_type FROM user limit ".$offset.","."$pageSize";

$res = $connect->query($sql);

$data = array();
if($res && $res->num_rows>0) {
    while($user = $res->fetch_assoc()) {
        $data[] = $user;
    }
}
//释放结果集
$res->free();

$sql = "SELECT COUNT(*) FROM user";
$res = $connect->query($sql);
$num = $res->fetch_row();

if($data) {
    return Response::show(200,$num[0],$data);
} else {
    return Response::show(400,'用户列表获取失败',$data);
}
